package com.xcf.controller.business.entities;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.entities.EntityMaintainService;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/entityMaintain")
public class EntityMaintainController extends BaseController{
	@Resource
	private EntityMaintainService entityMaintainService;
	String menuUrl = "entityMaintain/list";
	@RequestMapping(value="/list")
	public ModelAndView entityMaintainList()throws Exception{
		ModelAndView view = this.getModelAndView();
		PageData pd = new PageData();
		pd=this.getPageData();
		view.setViewName("/business/entities/entity_maintain_test");
		return view;
		
	}
	/**
	 * 往前端传值
	 * @param response
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value="/test")
	@ResponseBody
	public ResponseResult<Object> entityMaintainListInfo(int page,int limit)throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		PageHelper.startPage(page, limit);
		List<PageData> list = entityMaintainService.findEntityMaintainInfo(pd);		
		return ResponseResult.success(new PageInfo(list));
//		JSONArray json = new JSONArray();
//		for(PageData p : entityMainTain){
//			 JSONObject jo = new JSONObject();
//			 jo.put("ID", p.get("ID"));
//			 jo.put("ENTITIES_CODE", p.getString("ENTITIES_CODE"));
//			 jo.put("OVERALL_CODE", p.getString("ENTITIES_CODE"));
//			 jo.put("ENTITIES_FULLNAME", p.getString("ENTITIES_FULLNAME"));
//			 jo.put("ENTITIES_CLASS", p.getString("ENTITIES_CLASS"));
//			 jo.put("GUEST", p.getString("GUEST"));
//			 jo.put("FREIGHTER", p.getString("FREIGHTER"));
//			 jo.put("SUPPLIER", p.getString("SUPPLIER"));
//			 jo.put("INTERNAL_BRANCH", p.getString("INTERNAL_BRANCH"));
//			 jo.put("SETTLEMENT_CATEGORY_ID", p.get("SETTLEMENT_CATEGORY_ID"));
//			 jo.put("BUSINESS_CLASS_ID", p.getString("BUSINESS_CLASS_ID"));
//			 jo.put("SOCIAL_CREDIT_CODE", p.getString("SOCIAL_CREDIT_CODE"));
//			 jo.put("ENABLE", p.get("ENABLE"));
//			 jo.put("ENTITY_EN_NAME", p.getString("ENTITY_EN_NAME"));
//			 jo.put("ENTITY_ABBREVIATED", p.getString("ENTITY_ABBREVIATED"));
//			 jo.put("FINANCIAL_CONTACT", p.getString("FINANCIAL_CONTACT"));
//			 jo.put("FINANCIAL_CONTACT_PHONE", p.getString("FINANCIAL_CONTACT_PHONE"));
//			 jo.put("BANK_ACCOUNT", p.getString("BANK_ACCOUNT"));
//			 jo.put("RECEIPTS_PAYMENT_TYPE", p.getString("RECEIPTS_PAYMENT_TYPE"));
//			 jo.put("EMAIL", p.getString("EMAIL"));
//			 jo.put("BUSINESS_CONTACT", p.getString("BUSINESS_CONTACT"));
//			 jo.put("BUSINESS_CONTACT_PHONE", p.getString("BUSINESS_CONTACT_PHONE"));
//			 jo.put("ENTITY_SOURCE", p.getString("ENTITY_SOURCE"));
//			 jo.put("AMOUNT_RECEIVABLE", p.get("AMOUNT_RECEIVABLE"));
//			 jo.put("ACCOUNTS_RECEIVABLE_PERIOD", p.get("ACCOUNTS_RECEIVABLE_PERIOD"));
//			 jo.put("AMOUNT_PAYABLE", p.get("AMOUNT_PAYABLE"));
//			 jo.put("COPE_DEALLINE", p.get("COPE_DEALLINE"));
//			 
//			
//			 
//
//	            json.add(jo);
//		}
//		response.setContentType("text/html; charset=utf-8");
//		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"root"+"\""+":"+json.toString()+"}");				
//		response.getWriter().flush();
	}
	/**
	 * 往前端传值(合同)
	 * @param response
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value="/contractInfo")
	@ResponseBody
	public ResponseResult<Object> contractInfo(int page, int limit)throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		PageHelper.startPage(page, limit);
//		List<PageData> entityMainTain = entityMaintainService.findEntityMaintainInfo(pd);
		List<PageData>list = entityMaintainService.findContractInfo(pd);
		return ResponseResult.success(new PageInfo(list));
//		JSONArray json = new JSONArray();
//		for(PageData p : contractInfo){
//			 JSONObject jo = new JSONObject();
//			 jo.put("ID", p.get("ID"));
//			 jo.put("CONTRACT_NUM", p.getString("CONTRACT_NUM"));
//			 jo.put("CONTRACT_APPENDIX", p.getString("CONTRACT_APPENDIX"));
//			 jo.put("BUSINESS_CLASS_ID", p.getString("BUSINESS_CLASS_ID"));
//			 
//			 if(p.get("REGISTRATION_DATE")!=null || p.get("REGISTRATION_DATE")!=""){
//				 	
//			        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			        String REGISTRATION_DATE = sdf.format(p.get("REGISTRATION_DATE"));
//				 jo.put("REGISTRATION_DATE", REGISTRATION_DATE);
//			 }
//			 
//			 if(p.get("DEADLINE_DATE")!=null || p.get("DEADLINE_DATE")!=""){
//				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			     String DEADLINE_DATE = sdf.format(p.get("DEADLINE_DATE"));
//				 jo.put("DEADLINE_DATE",DEADLINE_DATE);
//			 }
//			 jo.put("CONTRACT_CONTENTS", p.getString("CONTRACT_CONTENTS"));
//			 jo.put("ENTITIES_CODE", p.getString("ENTITIES_CODE"));
//			 jo.put("ENTITY_ID", p.getString("ENTITY_ID"));
//			 if(p.get("UPDATE_DATE")!=null || p.get("UPDATE_DATE")!=""){
//				 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//			     String UPDATE_DATE = sdf.format(p.get("UPDATE_DATE"));
//				 jo.put("UPDATE_DATE",UPDATE_DATE);
//			 }
//			 
//			
//			 
//
//	            json.add(jo);
//		}
//		response.setContentType("text/html; charset=utf-8");
//		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"row"+"\""+":"+json.toString()+"}");				
//		response.getWriter().flush();
	}
	/**
	 * 往前端传值(发票抬头)
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/invoiceTitleInfo")
	public void invoiceTitleInfo(HttpServletResponse response)throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
//		List<PageData> entityMainTain = entityMaintainService.findEntityMaintainInfo(pd);
		List<PageData>invoiceTitle = entityMaintainService.findInvoiceTitle(pd);
		JSONArray json = new JSONArray();
		for(PageData p : invoiceTitle){
			 JSONObject jo = new JSONObject();
//			 	jo.put("id", p.getString("PARENT_DEPARTMENT_ID"));	  
//	            jo.put("text", p.getString("PARENT_DEPARTMENT_NAME"));

			 jo.put("ID", p.get("ID"));
			 jo.put("ENTITIES_FULLNAME", p.getString("ENTITIES_FULLNAME"));
			 jo.put("INVOICE_TITLE", p.getString("INVOICE_TITLE"));
			 jo.put("ENTITY_ID", p.getString("ENTITY_ID"));
	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"50"+"\""+","+"\""+"rows"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 保存或者更新信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveOrUpdateData")
	@ResponseBody
	public String saveOrUpdateData() throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		
		if(pd.getString("ID").isEmpty()){
			entityMaintainService.saveEntityMaintain(pd);
			}else{
				entityMaintainService.updateEntityMaintain(pd);
			}
		return null;
		
	}
	/**
	 * 保存或者更新信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveOrUpdateContractData")
	@ResponseBody
	public String saveOrUpdateContractData() throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		
		if(pd.getString("ID").isEmpty()){
			Date day=new Date();    
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			pd.put("UPDATE_DATE", df.format(day));
			entityMaintainService.saveContractInfo(pd);
			}else{
				Date day=new Date();    
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				pd.put("UPDATE_DATE", df.format(day));
				entityMaintainService.updateContractInfo(pd);
			}
		return null;
		
	}
	/**
	 * 删除信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteEntityMaintain")
	@ResponseBody
	public String deleteEntityMaintain()throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			entityMaintainService.deleteEntityMaintain(pdq);
		}
		return null;
		
	}
	/**
	 * 删除信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteContractInfo")
	@ResponseBody
	public String deleteContractInfo()throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			entityMaintainService.deleteContractInfo(pdq);
		}
		return null;
		
	}
	/**
	 * 删除信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInvoice")
	@ResponseBody
	public String deleteInvoice()throws Exception{
		PageData pd = new PageData();
		pd= this.getPageData();
		
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			entityMaintainService.deleteInfo(pdq);
		}
		return null;
		
	}
	/**
	 * 保存发票信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveInvoiceInfo")
	@ResponseBody
	public String saveInvoiceInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("records");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("ID").trim();

			String ENTITIES_FULLNAME = item.getString("ENTITIES_FULLNAME");
			String INVOICE_TITLE = item.getString("INVOICE_TITLE");
			String ENTITY_ID = pd.getString("ENTITY_ID");
			PageData pdq = new PageData();
			pdq.put("ID", id);
			pdq.put("ENTITIES_FULLNAME", ENTITIES_FULLNAME);
			pdq.put("INVOICE_TITLE", INVOICE_TITLE);
			pdq.put("ENTITY_ID", ENTITY_ID);
			
			if(id.isEmpty()){
				entityMaintainService.saveInfo(pdq);
			}else{
				entityMaintainService.updateInfo(pdq);
			}
			
		}
		return null;
		
	}
}
